﻿using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Rsdn.Janus.ObjectModel;
using WeifenLuo.WinFormsUI.Docking;

namespace Rsdn.Janus.Framework
{
    public partial class ContentDummyForm : DockContent
    {
        ManualResetEvent replaceControlReady = new ManualResetEvent(false);
        public ContentDummyForm()
        {
            InitializeComponent();
            this.DockAreas = DockAreas.Document;
            this.ShowHint = DockState.Document;
        }

        private void ReplaceControlInternal(Control oldAssociatedControl, Control value)
        {
            if (InvokeRequired)
            {
                MethodInvoker method = delegate { ReplaceControlInternal(oldAssociatedControl, value); };
                Invoke(method);
            }
            else
            {
                if (oldAssociatedControl != null)
                {
                    oldAssociatedControl.Visible = false;
                }

                Controls.Clear();
                if (value != null)
                {
                    value.Dock = DockStyle.Fill;
                    Controls.Add(value);
                    IFeatureView newFeatureView = value as IFeatureView;
                    if (newFeatureView != null && oldAssociatedControl != value)
                    {
                        newFeatureView.ActivateView();
                    }

                    // make control visible again because it is singleton
                    if (!value.Visible)
                    {
                        value.Visible = true;
                    }
#if SEL_TRACE
                    Trace.WriteLine(string.Format("SEL:Replace active control from {0} to {1}", oldAssociatedControl, value));
                    Trace.Flush();
#endif

                    replaceControlReady.Set();
                }
            }
        }

        /// <summary>
        /// Связанный с формой элемент управления.
        /// </summary>
        public Control AssociatedControl
        {
            get
            {
                return Controls.Count > 0 ? Controls[0] : null;
            }

            set
            {
                SuspendLayout();
                Control oldAssociatedControl = AssociatedControl;
                IFeatureView oldFeatureView = oldAssociatedControl as IFeatureView;

                if (oldFeatureView != null && oldAssociatedControl != value)
                {
                    oldFeatureView.DeactivateView();
                }

                replaceControlReady.Reset();
                // it is important to hide control for preventing bad data refresh
                ReplaceControlInternal(oldAssociatedControl, value);
                replaceControlReady.WaitOne();
                ResumeLayout();
            }
        }

        private void ContentDummyForm_Closing(object sender, CancelEventArgs e)
        {
            Controls.Clear();
        }

        public void SetTextThreadSafe(string text)
        {
            if (InvokeRequired)
            {
                MethodInvoker method = delegate { SetTextThreadSafe(text); };
                Invoke(method);
            }
            else
            {
                Text = text;
            }
        }
    }
}